//
//  Extension+UIImage.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

extension UIImage{
    
    class func emoticonImage(emoticonModel:HMEmoticonModel) -> UIImage?{
        //图片表情
        let png = emoticonModel.png ?? ""
        let path = emoticonModel.path ?? ""
        let image = UIImage(named:"\(path)\(png)", inBundle: HMEmoticonTool.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
        return image
    }
    
    
    /**
     等比例压缩
     上下文来实现
     */
    class func dealImageScale(image:UIImage,width:CGFloat) -> UIImage{
        //如果规定的宽度大于图片的宽度，直接返回图片
        if image.size.width < width {
            return image
        }
        //等比例后的高度
        let height = image.size.height*width/image.size.width
        //计算rect
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        //01 开启上下文
        UIGraphicsBeginImageContext(rect.size)
        //02 内容渲染到上下文
        image.drawInRect(rect)
        //03 从上下文获取内容
        let res = UIGraphicsGetImageFromCurrentImageContext()
        //04 关闭上下文
        UIGraphicsEndImageContext()
        return res
        
    }

}